﻿using MvvmFoundation.Wpf;
using ServiceOperater.Behaviors;
using ServiceOperater.Helpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace ServiceOperater.ViewModels
{
    public sealed class ActionTreeNodeViewModel : ObservableObject, IFeedback
    {

        public ActionTreeNodeViewModel(string name, ServiceTreeNodeViewModel parent)
        {
            this.Name = name;
            this.Parent = parent;
            this.FoundFileInfoList = new ObservableCollection<FoundFileInfo>();

            instantiateCommand();
        }

        private void instantiateCommand()
        {
            ResetActionRecordCommand = new RelayCommand<ActionTreeNodeViewModel>((currentAtnVm) =>
            {
                App.Messenger.NotifyColleagues(MessengerToken.TRANSACTION_ROLLBACK, currentAtnVm);
            }, (currentAtnVm) =>
            {
                return true;
            });

            SaveActionRecordCommand = new RelayCommand<ActionTreeNodeViewModel>((currentAtnVm) =>
            {
                // 去某处获取action原始的name, service原始的id
                App.Messenger.NotifyColleagues(MessengerToken.TRANSACTION_RETRIEVAL, currentAtnVm);
                App.Messenger.NotifyColleagues(MessengerToken.TRANSACTION_RETRIEVAL, currentAtnVm.Parent);

                ServiceFileOperater.SaveActionNode(currentAtnVm, currentAtnVm.Parent.LocationFile.FullPath);
                App.Messenger.NotifyColleagues(MessengerToken.TRANSACTION_APPLY, currentAtnVm);
            }, (currentAtnVm) =>
            {
                return true;
            });
        }

        #region Props
        // 我们拿Name作为唯一性标识, 而该Name是可能被用户更改的
        public String OriginName { get; set; }

        // 完整属性参见DAPT项目的service-common.xml文件
        private string name = string.Empty;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                if (name == value)
                {
                    return;
                }

                name = value;

                // TODO 保存起来！

                RaisePropertyChanged("Name");
            }
        }

        private string sqlId = string.Empty;
        public string SqlId
        {
            get
            {
                return sqlId;
            }
            set
            {
                if (sqlId == value)
                {
                    return;
                }

                sqlId = value;

                // TODO 保存起来！

                RaisePropertyChanged("SqlId");
            }
        }

        private string sqlType = string.Empty;
        public string SqlType
        {
            get
            {
                return sqlType;
            }
            set
            {
                if (sqlType == value)
                {
                    return;
                }

                sqlType = value;

                // TODO 保存起来！

                RaisePropertyChanged("SqlType");
            }
        }

        private string result = string.Empty;
        public string Result
        {
            get
            {
                return result;
            }
            set
            {
                if (result == value)
                {
                    return;
                }

                result = value;

                // TODO 保存起来！

                RaisePropertyChanged("Result");
            }
        }

        private string resultType = string.Empty;
        public string ResultType
        {
            get
            {
                return resultType;
            }
            set
            {
                if (resultType == value)
                {
                    return;
                }

                resultType = value;

                // TODO 保存起来！

                RaisePropertyChanged("ResultType");
            }
        }

        private string parameter = string.Empty;
        public string Parameter
        {
            get
            {
                return parameter;
            }
            set
            {
                if (parameter == value)
                {
                    return;
                }

                parameter = value;

                // TODO 保存起来！

                RaisePropertyChanged("Parameter");
            }
        }

        private bool? isPage = null;
        public bool? IsPage
        {
            get
            {
                return isPage;
            }
            set
            {
                if (isPage == value)
                {
                    return;
                }

                isPage = value;

                // TODO 保存起来！

                RaisePropertyChanged("IsPage");
            }
        }

        private string handler = string.Empty;
        public string Handler
        {
            get
            {
                return handler;
            }
            set
            {
                if (handler == value)
                {
                    return;
                }

                handler = value;

                // TODO 保存起来！

                RaisePropertyChanged("Handler");
            }
        }

        private string condition = string.Empty;
        public string Condition
        {
            get
            {
                return condition;
            }
            set
            {
                if (condition == value)
                {
                    return;
                }

                condition = value;

                // TODO 保存起来！

                RaisePropertyChanged("Condition");
            }
        }

        private string refenerce = string.Empty;
        public string Ref
        {
            get
            {
                return refenerce;
            }
            set
            {
                if (refenerce == value)
                {
                    return;
                }

                refenerce = value;

                // TODO 保存起来！

                RaisePropertyChanged("Ref");
            }
        }

        // ----
        public ServiceTreeNodeViewModel Parent { get; private set; }

        private bool isModified = false;
        public bool IsModified
        {
            get
            {
                return isModified;
            }
            set
            {
                if (isModified == value)
                {
                    return;
                }

                isModified = value;
                this.Parent.IsModified = isModified;
                this.Parent.LocationFile.IsModified = isModified;

                RaisePropertyChanged("IsModified");
            }
        }

        // 作为tree的节点数据源时, 是否处于编辑状态
        private bool isInEditMode = false;
        public bool IsInEditMode
        {
            get
            {
                return isInEditMode;
            }
            set
            {
                if (isInEditMode == value)
                {
                    return;
                }

                isInEditMode = value;

                RaisePropertyChanged("IsInEditMode");
            }
        }

        // 指示该service是否在文件中有相应的内容
        private bool isVirtual = false;
        public bool IsVirtual
        {
            get
            {
                return isVirtual;
            }
            set
            {
                if (isVirtual == value)
                {
                    return;
                }

                isVirtual = value;

                RaisePropertyChanged("IsVirtual");
            }
        }

        // 引用这个service的文件相关位置信息
        public ObservableCollection<FoundFileInfo> FoundFileInfoList { get; private set; }

        private bool isExpanded = false;
        public bool IsExpanded
        {
            get
            {
                return isExpanded;
            }
            set
            {
                if (isExpanded == value)
                {
                    return;
                }

                isExpanded = value;

                RaisePropertyChanged("IsExpanded");
            }
        }

        private bool isSelected = false;
        public bool IsSelected
        {
            get
            {
                return isSelected;
            }
            set
            {
                if (isSelected == value)
                {
                    return;
                }

                isSelected = value;

                RaisePropertyChanged("IsSelected");
            }
        }


        #endregion

        #region Commands

        public RelayCommand<ActionTreeNodeViewModel> ResetActionRecordCommand { get; private set; }
        public RelayCommand<ActionTreeNodeViewModel> SaveActionRecordCommand { get; private set; }
        #endregion

        #region 枚举数据源
        public IEnumerable<String> SqlTypeList
        { get { return new List<String>() { "select", "insert", "update", "delete" }; } }

        public IEnumerable<String> ResultTypeList
        { get { return new List<String>() { " ", "list", "single" }; } }

        public IEnumerable<Boolean?> TrueFalseList
        { get { return new List<Boolean?>() { true, false }; } }
        //public IEnumerable<ValueDescription> QXDMList
        //{ get { return EnumHelper.GetAllValuesAndDescriptions<QXDM>(); } }

        //public IEnumerable<ValueDescription> DJLXList
        //{ get { return EnumHelper.GetAllValuesAndDescriptions<DJLX>(); } }
        #endregion

        // Interface
        void IFeedback.Feedback(Dictionary<string, Tuple<object, object>> modifyVals, Object dataContext)
        {
            if (dataContext != this)
            {
                return;
            }

            bool anyChange = false;
            foreach (KeyValuePair<String, Tuple<object, object>> keyVal in modifyVals)
            {
                var oldVal = keyVal.Value.Item1;
                var newVal = keyVal.Value.Item2;
                if (oldVal is IComparable)
                {
                    if (((IComparable)oldVal).CompareTo(newVal) != 0)
                    {
                        anyChange = true;
                        break;
                    }

                }
            }

            this.IsModified = anyChange;
        }


        void IFeedback.RetrievalChange(Dictionary<string, Tuple<object, object>> modifyVals, Object dataContext)
        {
            if (dataContext != this)
            {
                return;
            }

            // 我们需要原始name去更新文件
            var nameStr = ReflectHelper.GetMemberName((ActionTreeNodeViewModel c) => c.Name);
            if (!modifyVals.ContainsKey(nameStr))
            {
                return;
            }

            var originValOfName = modifyVals[nameStr].Item1;

            this.OriginName = originValOfName.ToString();
        }
    }





}
